#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->head = NULL;
	q->tail = NULL;
	q->size = 0;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->head)
	{
		QNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
	q->head = NULL;
	q->tail = NULL;
	q->size = 0;
	
}

void EnQueue(Queue* q, QueueDataType BTreeNode)
{
	assert(q);
	QNode* qnode = (QNode*)malloc(sizeof(QNode));
	if (qnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	qnode->next = NULL;
	qnode->qdata = BTreeNode;

	if (q->head == NULL)
	{
		q->head = qnode;
	}
	else
	{
		q->tail->next = qnode;
	}
	q->tail = qnode;
	q->size++;
}

void DeQueue(Queue* q)
{
	assert(q);
	assert(!QueueIsEmpty(q));
	
	QNode* _next = q->head->next;
	free(q->head);
	q->head = _next;
	if (q->head == NULL)
	{
		q->tail = NULL;
	}
	q->size--;
}

QueueDataType QueuePeek(Queue* q)
{
	assert(q);
	assert(!QueueIsEmpty(q));
	return q->head->qdata;
}

bool QueueIsEmpty(Queue* q)
{
	assert(q);
	return q->size == 0;
}